#include "videog.h"
#include <stdlib.h>
#include <stdio.h>
#include <dpmi.h>
#include <go32.h>
#include <dos.h>
#include "utypes.h"
#include "ints.h"
#include "kbc.h"
#include "tab.h"
#include "chtable.c"
#include "GQueue.h"


#ifdef EMBED
        #include "libepc.h"

        #define getKey() ScanCode2Ascii(GetScanCode())
        #define hasKey() ScanCodeRdy()
        #define writeChar(ch) PutChar((ch))
        #define setCursor(row, col) SetCursorPosition((row), (col))
        #define printString(msg) PutString((msg))
        
        #define MAIN void main(long v_mem)

#else
        #define getKey() getch()
        #define hasKey() kbhit()
        #define writeChar(ch) printf('%c', (ch))
        #define setCursor(row, col) gotoxy((col), (row))
        #define printString(msg) printf("%s", msg)
        #define MAIN int main(int argc, char** argv)
#endif


//int HRES, VRES;
//int v = 0;

// Variaveis globais
char * video_mem = NULL;
char * image_buffer = NULL;
char * image_buffer_original = NULL;
__dpmi_meminfo video_map;
char * char_table;
volatile int scancode = 0;

void kbc_handler(void);
extern void t0_isr();
Queue queue_keyboard;
_go32_dpmi_seginfo timer0_new_irq;
_go32_dpmi_seginfo timer0_old_irq;
_go32_dpmi_seginfo keyboard_new_irq;
_go32_dpmi_seginfo keyboard_old_irq;
_go32_dpmi_seginfo mouse_old_irq;

#define video_mode 105

int main(int argc, char** argv){

        
        
        // Arranque do necessario inicial
	char_table = (char*) malloc(sizeof(char)*256*16);
	load_char_table(char_table);
	video_mem = enter_graphics(video_mode, &video_map);
	image_buffer = (char*)malloc(HRES*VRES*sizeof(char));
	image_buffer_original = (char*)malloc(HRES*VRES*sizeof(char));
	reset_screen(BLACK, image_buffer_original);
	queueInit(&queue_keyboard, 100);

	srand((unsigned)time(NULL));
	disable_irq(KBD_IRQ);
	kbc_init(0);
	
	// Iniciacao do timer e do jogo
	//initTimer0(t0_isr);

	initKeyBoard(handler_keyboard);
	

	enable_irq(KBD_IRQ);
	
	// Iniciacao do timer e do jogo
	initTimer0(t0_isr);
	
	//start();
	menu();

	endKeyBoard();
	endMouse();
	//endTimer0();
	

	//freeJogo();
	free(image_buffer);
	leave_graphics ( &video_map );
        
        
        return 0;
}




